'''
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/submissions/
做一个层次计数
当一个节点没有左右子节点时返回该计数
'''
from collections import deque
from util.TreeHelper import *


class Solution:
    def minDepth(self, root: TreeNode) -> int:
        queue = deque()
        if root:
            queue.append(root)
        res = 0
        while queue:
            res += 1
            for _ in range(0, len(queue)):
                root = queue.popleft()
                if root:

                    if not root.left and not root.right:
                        return res
                    queue.append(root.left)
                    queue.append(root.right)
        return res
